找传奇、传世资源到传世资源站!

安卓下调用MQtt实现消息推送

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardpackage cn.tyys.mqttbroker.client;import android.os.Handler;import android.os.Message;import android.util.Log;import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;import org.eclipse.paho.client.mqttv3.MqttCallback;import org.eclipse.paho.client.mqttv3.MqttClient;import org.eclipse.paho.client.mqttv3.MqttConnectOptions;import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;import org.eclipse.paho.client.mqttv3.MqttException;import org.eclipse.paho.client.mqttv3.MqttMessage;import org.eclipse.paho.client.mqttv3.MqttTopic;import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;/** * Created by lhq on 2018/5/21. */public class ApolloClient {    private static String host;    private static String userName = "admin";    private static String passWord = "password";    private static MqttClient client;    private Handler handler;    private static String send_topic ;    private static String receive_topic ;    public ApolloClient(Handler handler,String host,String send_topic,String receive_topic) {        this.handler=handler;        this.host=host;        this.send_topic=send_topic;        this.receive_topic=receive_topic;    }    public void createContent() throws MqttException {        //host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,        //MemoryPersistence设置clientid的保存形式,默认为以内存保存        client = new MqttClient(host, "client", new MemoryPersistence());        MqttConnectOptions options = new MqttConnectOptions();        //设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,        //这里设置为true表示每次连接到服务器都以新的身份连接        options.setCleanSession(true);        //设置连接的用户名        options.setUserName(userName);        //设置连接的密码        options.setPassword(passWord.toCharArray());        // 设置超时时间 单位为秒        options.setConnectionTimeout(10);        // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制        options.setKeepAliveInterval(20);        //回调        client.setCallback(new MqttCallback() {            @Override            public void connectionLost(Throwable cause) {                // //连接丢失后,一般在这里面进行重连                Log.d("lhq","connectionLost----------");                Message obtainMessage = handler.obtainMessage();                obtainMessage.what=0;                obtainMessage.obj=cause.getMessage();                handler.sendMessage(obtainMessage);            }            @Override            public void messageArrived(String topic, MqttMessage message) throws Exception {                //subscribe后得到的消息会执行到这里面                Log.d("lhq","messageArrived----------");                Log.d("lhq",topic "---" message.toString());                Message obtainMessage = handler.obtainMessage();                obtainMessage.what=1;                obtainMessage.obj=message.toString();                handler.sendMessage(obtainMessage);            }            @Override            public void deliveryComplete(IMqttDeliveryToken token) {                //publish后会执行到这里                Log.d("lhq","deliveryComplete---------" token.isComplete());            }        });        //链接        client.connect(options);        //订阅        client.subscribe(receive_topic, 1);    }    public void publish(String meg) throws MqttException {        MqttTopic topic = client.getTopic(send_topic);        MqttMessage message = new MqttMessage();        message.setQos(1);        message.setRetained(true);        Log.d("lhq",message.isRetained() "------ratained状态");        message.setPayload(meg.getBytes());        MqttDeliveryToken token = topic.publish(message);        token.waitForCompletion();    }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复